package com.adam.masterSpringMVC4.controller;

import com.adam.masterSpringMVC4.weibo.ParallelSearchService;
import com.adam.masterSpringMVC4.weibo.WeiboData;
import com.adam.masterSpringMVC4.weibo.WeiboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class SearchController {

    private WeiboService weiboService;
    private ParallelSearchService parallelSearchService;

    @Autowired
    public SearchController(WeiboService weiboService, ParallelSearchService parallelSearchService) {
        this.weiboService = weiboService;
        this.parallelSearchService = parallelSearchService;
    }

    @RequestMapping("/search/{searchType}")
    public ModelAndView search(@PathVariable("searchType")String searchType, @MatrixVariable List<String> keywords) {
        List<WeiboData> weiboDataList = parallelSearchService.search(searchType, keywords);
        ModelAndView modelAndView = new ModelAndView("resultPage");
        modelAndView.addObject("weibos", weiboDataList);
        modelAndView.addObject("keyword", String.join(",", keywords));
        return modelAndView;
    }

}
